import React, { useState, useEffect } from 'react';
import styles from './index.less';
import CardCommon from '@/compontents/cardCommon';
import lhTool from 'lh-tool';
import api from '@/http/api';
import { ExclamationCircleOutlined } from '@ant-design/icons';
import TablePage from '@/compontents/tablePage';
import { Button, Modal } from 'antd';
const { confirm } = Modal;
export default (props: any) => {
  const { manageId, type } = JSON.parse(sessionStorage.info).userInfo;
  const col = [
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      align: 'center',
      width: 100,
      render: (text: any, record: any, index: any) => `${index + 1}`,
    },
    {
      title: '标题',
      dataIndex: 'title',
      width: 200,
      ellipsis: true,
    },
    {
      title: '简介',
      dataIndex: 'intro',
      width: 300,
      ellipsis: true,
    },
    {
      title: '活动发布时间',
      dataIndex: 'publishTime',
      render: (v1: any) =>
        lhTool.formatDate.format(new Date(v1), 'yyyy/MM/dd hh:mm'),
    },
    {
      title: '活动结束时间',
      dataIndex: 'endTime',
      render: (v1: any) =>
        lhTool.formatDate.format(new Date(v1), 'yyyy/MM/dd hh:mm'),
    },
    {
      title: '活动地址',
      dataIndex: 'address',
      width: 100,
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'handle',
      width: 200,
      render: (v1: any, v2: any) => (
        <div>
          <Button
            onClick={() => {
              toAdd(0, v2);
            }}
            size="small"
            type="primary"
          >
            编辑
          </Button>
          <Button
            onClick={() => {
              toDelete(v2);
            }}
            style={{ marginLeft: 10 }}
            size="small"
            danger
            type="primary"
          >
            删除
          </Button>
        </div>
      ),
    },
  ];
  const [list, setList] = useState([]);
  const getList = () => {
    api
      .uniGet({
        manageId,
        aType: type,
      })
      .then(res => {
        const _data: any = [...res.data];
        _data.map((item: any) => {
          item.key = item.id;
        });
        setList(_data);
      });
  };
  const toAdd = (v: any, v2: any) => {
    props.history.push({
      pathname: '/uni/add',
      state: {
        isAdd: v,
        initData: v ? {} : v2,
      },
    });
  };
  const toDelete = (v2: any) => {
    confirm({
      title: '提示',
      icon: <ExclamationCircleOutlined />,
      content: '确定删除吗？',
      onOk() {
        api.uniDelete({ id: v2.id }).then(res => {
          getList();
        });
      },
    });
  };
  useEffect(() => {
    getList();
  }, []);
  return (
    <CardCommon>
      <div className={styles.wrap}>
        {type !== 0 && (
          <div className={styles.btnW}>
            <Button
              onClick={() => {
                toAdd(1, {});
              }}
              type="primary"
            >
              新增
            </Button>
          </div>
        )}
        <TablePage col={col} list={list} />
      </div>
    </CardCommon>
  );
};
